﻿using Sui.Domain.Domain;
using Sui.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sui.Domain.App
{
    public class PublicServerApp : AppBase
    {
        private IPublicServerRepository service = new PublicServerRepository(conn);
        public int Save(PublicServerEntity entity, out string id, out DateTime? creator_time)
        {
            DataTable dt= GetDataTableBySql($"select *from public_server where ip2='{entity.ip2}' and port2={entity.port2}");
            if (dt.Rows.Count == 0)
            {
                entity.Create();
                id = entity.id;
                creator_time = entity.creator_time;
                entity.modify_time = creator_time;
                return service.Insert(entity);
            }
            else
            {
                entity.id = dt.Rows[0][0].ToString();
                DateTime dtime =DateTime.Now;
                bool ret= DateTime.TryParse(dt.Rows[0]["creator_time"].ToString(),out dtime);
                if (dtime.ToString().StartsWith("000"))
                {
                    dtime = DateTime.Now;
                }
                entity.creator_time = dtime;
                entity.Modify(entity.id);
                id = entity.id;
                creator_time = entity.creator_time;
                return service.Update(entity);
            }
        }
        public int Save(PublicServerEntity entity)
        {
            string id = "";
            DateTime? creator_time = DateTime.Now;
            return Save(entity, out id, out creator_time);
        }
    }
}
